package ee.serge.client.content.cp.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.RichTextArea;

import ee.serge.client.AlertBox;
import ee.serge.client.Move;
import ee.serge.service.Service;
import ee.serge.shared.Article;
import ee.serge.util.AdminContent;
import ee.serge.util.RichTextToolbar;
import ee.serge.util.SimpleCallback;
import ee.serge.util.URLData;
import ee.serge.util.bootstrap.BButton;
import ee.serge.util.bootstrap.BButtonBar;

public class ArticlesContent extends AdminContent {

   public static final String TYPE_HOW_TO_CHOOSE_A_LOAN = "howtochoosealoan";
   public static final String TYPE_LOANERS_RIGHTS = "loandersrights";
   public static final String TYPE_CONTACT = "contact";
   private static final String NAME = "cp/borrowers_rights";
   
   public static String getName() {
      return NAME;
   }
   
   public static URLData getURLData(String type) {
      URLData data = new URLData();
      data.add("type", type);
      data.setName(NAME);
      return data;
   }
   
   public static void show() {
      type = Move.get().getCurrentURLData().get("type");
      Service.getArticleService().getArticle(type, new SimpleCallback<Article>() {

         @Override
         public void success(Article result) {
            Move.get().setContent(new ArticlesContent(result));
         }
      });
   }
   
   public ArticlesContent(Article result) {
      buildPanel(result);
   }
   
   private RichTextArea textArea;
   private static String type;
   
   private void buildPanel(Article article) {
      if (type.equals(TYPE_HOW_TO_CHOOSE_A_LOAN)) {
         setHeader("Choosing a lender");
      } else {
         setHeader("Know your rights");
      }
      
      textArea = new RichTextArea();
      textArea.setHTML(article.getText());
      textArea.setSize("100%", "450px");
      RichTextToolbar toolbar = new RichTextToolbar(textArea);
      toolbar.ensureDebugId("cwRichText-toolbar");
      toolbar.setWidth("100%");
      
      Grid grid = new Grid(2, 1);
      grid.setWidth("100%");
      grid.setStyleName("cw-RichText");
      grid.setWidget(0, 0, toolbar);
      grid.setWidget(1, 0, textArea);
      add(grid);
      
      BButtonBar buttonBar = new BButtonBar();
      buttonBar.add(new BButton("Save", BButton.SUCCESS, new SaveHandler()));
      add(buttonBar);
   }
   
   private Article getArticle() {
      System.out.println(textArea.getHTML());
      return new Article(type, textArea.getHTML());
   }
   
   private class SaveHandler implements ClickHandler {
      @Override
      public void onClick(ClickEvent event) {
         Service.getArticleService().updateArticle(getArticle(), new SimpleCallback<Void>() {

            @Override
            public void success(Void result) {
               AlertBox.showSuccess("Article saved");
            }
         });
      }
   }
   
}
